<?php
namespace api\user\service\UserServer;

use api\user\controller\PublicController;
use think\Db;

class AdminStrategy extends userStrategy
{

    /**
     * [createUser 创建用户]
     * @Author   HUI
     * @DateTime 2019-01-08
     * @version  [version]
     * @param    array      $data [description]
     * @return   [type]           [description]
     */
    public function createUser($data = [])
    {
        $publics = new PublicController();
        $name = cmf_check_mobile($data['username']) ? 'mobile' : 'user_email';
        $count = Db::name("user")->where($name, $data['username'])->count();
        if ($count) {
            return 0;
        }
        
        $user = [
            'user_pass' => cmf_password($data['password']),
            'user_code' => $publics->userCode(),
            $name => $data['username'],
            'device_num' => $data['device_num'],
            'user_type' => empty($data['user_type']) ? 2 : $data['user_type'],
            'user_status' => 1,
            'create_time' => time(),
        ];

        if (Db::name("user")->insert($user)) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * [editUser 编辑用户]
     * @Author   HUI
     * @DateTime 2019-01-08
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function editUser($data = [])
    {
        $publics = new PublicController();
        // $name = cmf_check_mobile($data['username']) ? 'mobile' : 'user_email';
        $user = [
            'user_pass' => cmf_password($data['password']),
            'user_code' => $publics->userCode(),
            // $name => $data['username'],
            'device_num' => $data['device_num'],
        ];
        if (Db::name("user")->where('id', $data['user_id'])->update($user)) {
            return true;
        } else {
            return false;
        }
    }

    public function killUser($data = [])
    {
        $user['user_status'] = 0;
        if (Db::name("user")->where('id', $data['user_id'])->update($user)) {
            return true;
        } else {
            return false;
        }
    }

    public function releaseUser($data = [])
    {
        $user['user_status'] = 1;
        if (Db::name("user")->where('id', $data['user_id'])->update($user)) {
            return true;
        } else {
            return false;
        }
    }

    
    public function getUserInfo($data = [])
    {
        return Db::name("user")->where('id', $data['user_id'])->find();
    }


    public function getAllUser($data = [])
    {
    	return Db::name("user")->where('user_type', 'NEQ', 1)->paginate(10);
    }
}
